package com.app.art.service.mq;

import com.rabbitmq.client.Channel;
import com.app.art.config.RabbitMQConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Slf4j
@Component
public class MessageConsumer {

    @RabbitListener(queues = RabbitMQConfig.QUEUE_NAME)
    public void receiveMessage(Object message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) throws IOException, IOException {
        try {
            // 处理业务逻辑
            log.info("MessageConsumer :收到消息: {} , {} , {}", message, channel, deliveryTag);

            // 手动 ACK
            channel.basicAck(deliveryTag, false);
        } catch (Exception e) {
            // 拒绝消息，不重新入队（或根据业务决定）
            channel.basicNack(deliveryTag, false, false);
            // 或者重新入队：channel.basicNack(deliveryTag, false, true);
        }
    }
}
